package org.jeecg.modules.demo.mes.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.demo.mes.entity.MesMaterial;
import org.jeecg.modules.demo.mes.entity.MesMaterialDetails;
import org.jeecg.modules.demo.mes.mapper.MesMaterialMapper;
import org.jeecg.modules.demo.mes.service.IMesMaterialDetailsService;
import org.jeecg.modules.demo.mes.service.IMesMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description: 物料表
 * @Author: jeecg-boot
 * @Date:   2022-10-27
 * @Version: V1.0
 */
@Service
public class MesMaterialServiceImpl extends ServiceImpl<MesMaterialMapper, MesMaterial> implements IMesMaterialService {

    @Autowired
    private IMesMaterialDetailsService mesMaterialDetailsService;

    /**
     * 保存物料
     */
    @Override
    @Transactional(readOnly = false,isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
    public void saveMesMaterial(MesMaterial mesMaterial,String loginName) throws JeecgBootException {
        final Date now = new Date();
        mesMaterial.setUpdateBy(loginName);
        mesMaterial.setUpdateTime(now);
        //保存主表
        super.save(mesMaterial);
        //保存物料表之前，先保存附表
        final String materialSequence = mesMaterial.getMaterialSequence();
        final Integer materialNumber = mesMaterial.getMaterialNumber();
        if (materialNumber!=null && StringUtils.isNotBlank(materialSequence)){
            final List<MesMaterialDetails> detailsList = new ArrayList<>();
            for (int i = 0; i < materialNumber; i++) {
                final MesMaterialDetails details = new MesMaterialDetails();
//                details.setId(UUID.randomUUID().toString().replace("-",""));
                details.setCreateBy(loginName);
                details.setCreateTime(now);
                details.setUpdateBy(loginName);
                details.setUpdateTime(now);
                details.setMainId(mesMaterial.getId());
                details.setMaterialStatus("1");
                details.setMaterialBarCode(materialSequence+String.format("%03d", i));
                detailsList.add(details);
            }
            mesMaterialDetailsService.saveBatch(detailsList);
        }

    }

    @Override
    @Transactional(readOnly = false,isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
    public void updateMesMaterialById(MesMaterial mesMaterial) throws JeecgBootException {
        final String id = mesMaterial.getId();
        if (StringUtils.isBlank(id)){
            return;
        }
        //判断是否是修改的物料号
        final MesMaterial mesMaterialFromDB = this.getById(id);
        if (!StringUtils.equals(mesMaterial.getMaterialSequence(),mesMaterialFromDB.getMaterialSequence())){
            //说明编辑了物料号，需要同步更新详情表中的数据
            updateMesMaterialDetailsByMainId(id,mesMaterial.getMaterialSequence());
        }
        //修改物料表
        this.updateById(mesMaterial);
    }

    /**
     * 同步更新物料详情表中的数据
     */
    private void updateMesMaterialDetailsByMainId(String mainId, String materialSequence) {
        final Map<String,Object> cxMap = new HashMap<>();
        cxMap.put("mainId",mainId);
        final List<MesMaterialDetails> list = mesMaterialDetailsService.queryMaterialDetailsSelective(cxMap);
        if (list!=null && list.size()==0){
            return;
        }
        for (MesMaterialDetails details : list) {
            final String materialBarCode = details.getMaterialBarCode();
            details.setMaterialBarCode(materialSequence+ materialBarCode.substring(materialBarCode.length()-3));
        }
        mesMaterialDetailsService.updateBatchById(list);
    }

    /**
     * 通用物料表查询
     */
    @Override
    public List<MesMaterial> queryMesMaterialsSelective(Map<String, Object> cxMap) throws JeecgBootException {
        return this.baseMapper.queryMesMaterialsSelective(cxMap);
    }

    @Override
    @Transactional(readOnly = false,isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
    public void removeMesMaterialById(String id) throws JeecgBootException {
        this.removeById(id);
        mesMaterialDetailsService.removeByMainId(id);
    }
}
